from flask import Flask
from flask_basicauth import BasicAuth
from flask_compress import Compress
from flask_login import LoginManager
from flask_minify import Minify
from flask_sqlalchemy import SQLAlchemy

from app.database.base import Base, metadata

db = SQLAlchemy(metadata=metadata)
login_manager = LoginManager()
http_auth = BasicAuth()
compress = Compress()


def create_app():

    app = Flask(__name__, instance_relative_config=False)
    from app.server import config

    app.config.from_object(config.ProdConfig)

    db.init_app(app)
    login_manager.init_app(app)
    http_auth.init_app(app)
    compress.init_app(app)
    minify = Minify(app, html=True, js=False, cssless=True)

    with app.app_context():
        from app.server.flask_app import auth, routes
        from app.server.flask_app.admin_views import init_admin_panel
        from app.server.flask_app.assets import compile_static_assets

        init_admin_panel(app)

        app.register_blueprint(routes.main_bp)
        app.register_blueprint(auth.auth_bp)

        compile_static_assets(app)

        return app
